<template>
  <div class="salary-list">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="Home" :replace="true">首页</el-breadcrumb-item>
        <el-breadcrumb-item>员工薪资列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="searchParams">
      <el-form-item label="员工姓名" prop="employeeName">
        <el-input v-model="searchParams.employeeName" placeholder="请选择员工姓名" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findEmployee"></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pageData.list"
      border
      v-loading="pageData.loading"
      size="mini"
      header-cell-class-name="table-header-cell-font"
      cell-class-name="table-cell-font">
      <el-table-column prop="employeeName" label="员工姓名"></el-table-column>
      <el-table-column prop="basicSalary" label="基本工资"></el-table-column>
      <el-table-column prop="performanceSalary" label="绩效工资"></el-table-column>
      <el-table-column prop="bonus" label="奖金"></el-table-column>
      <el-table-column prop="allowance" label="津贴"></el-table-column>
      <el-table-column prop="overtimePay" label="加班费"></el-table-column>
      <el-table-column prop="socialSecurityDeduction" label="社保扣除"></el-table-column>
      <el-table-column prop="housingFundDeduction" label="公积金扣除"></el-table-column>
      <el-table-column prop="personalIncomeTaxDeduction" label="个人所得税扣除"></el-table-column>
      <el-table-column prop="otherDeductions" label="其他扣除"></el-table-column>
      <el-table-column prop="grossSalary" label="应发工资"></el-table-column>
      <el-table-column prop="netSalary" label="实发工资"></el-table-column>
      <el-table-column prop="payDate" label="发放日期" width="120"></el-table-column>
      <el-table-column prop="paymentMethod" label="支付方式">
        <template v-slot="{ row }">
          <div v-if="row.paymentMethod === 10">银行转账</div>
          <div v-if="row.paymentMethod === 20">现金</div>
          <div v-if="row.paymentMethod === 30">支票</div>
        </template>
      </el-table-column>
      <el-table-column prop="bankAccountNumber" label="银行账号" width="150"></el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="{ row }">
          <el-button @click.native.prevent="editRow(row.id)" type="primary">编辑</el-button>
          <el-button @click.native.prevent="delRow(row.id)" type="danger" plain>删除</el-button>
          <el-button @click.native.prevent="detailRow(row.id)" type="primary" plain>详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination">
      <el-pagination
        @current-change="paginationChange"
        :current-page="pageData.current"
        :page-sizes="pageData.pageSizes"
        :page-size="pageData.size"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageData.count">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="employeeSelectorVisible" v-if="employeeSelectorVisible" title="员工基本信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <employee-selector @employee-selected-event="handleEmployeeSelectedEvent">
      </employee-selector>
    </el-dialog>
    <el-dialog :visible.sync="salaryAddVisible" v-if="salaryAddVisible" :title="salaryAddTitle" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <salary-add :id="selectedSalaryId" @close-salary-add-event="handleCloseSalaryAddEvent" @reset-salary-add-event="handleResetSalaryAddEvent">
      </salary-add>
    </el-dialog>
    <el-dialog :visible.sync="salaryViewVisible" v-if="salaryViewVisible" title="员工薪资详情" :top="dialogTop" :width="dialogWidth">
      <salary-view :id="selectedSalaryId" @close-salary-view-event="handleCloseSalaryViewEvent">
      </salary-view>
    </el-dialog>
  </div>
</template>

<script>
import salaryApi from '@/api/salaryApi'
import EmployeeSelector from "@/views/employee/EmployeeSelector.vue";
import SalaryAdd from "@/views/salary/SalaryAdd.vue"
import SalaryView from "@/views/salary/SalaryView.vue"
import listQueryMixin from '@/mixins/listQueryMixin'
import {getEmployeePaymentMethodText} from "@/utils/dictTranslator";
export default {
  name: 'SalaryList',
  components: {EmployeeSelector,SalaryAdd, SalaryView},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        employeeId: '',
        employeeName: '',
      },
      employeeSelectorVisible: false,
      salaryAddVisible: false,
      salaryAddTitle: '',
      salaryViewVisible: false,
      selectedSalaryId: 0
    }
  },
  methods: {
    getPageData (p) {
      return salaryApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        employeeId: '',
        employeeName: '',
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    onAdd () {
      this.selectedSalaryId = ''
      this.salaryAddVisible = true
      this.salaryAddTitle = '员工薪资新增'
    },
    onExport () {
      const headers = ['员工姓名','基本工资','绩效工资','奖金','津贴','加班费','社保扣除','公积金扣除','个人所得税扣除','其他扣除','应发工资','实发工资','发放日期','支付方式','银行账号']
      const params = Object.assign(this.getPaginationParams(), this.searchParams)
      this.getPageData(params).then(data => {
        if (!data || !data.data || data.data.list.length < 1) {
          this.$message.error('无数据导出')
          return
        }
        const exportData = []
        for (const d of data.data.list) {
          exportData.push([d.employeeName,d.basicSalary,d.performanceSalary,d.bonus,d.allowance,d.overtimePay,d.socialSecurityDeduction,d.housingFundDeduction,d.personalIncomeTaxDeduction,d.otherDeductions,d.grossSalary,d.netSalary,d.payDate,getEmployeePaymentMethodText(d.paymentMethod),d.bankAccountNumber])
        }
        this.exportToExcel(headers, exportData)
      })
    },
    editRow (id) {
      this.selectedSalaryId = id
      this.salaryAddVisible = true
      this.salaryAddTitle = '员工薪资编辑'
    },
    delRow (id) {
      if (!id) {
        return
      }
      this.$confirm('确定要删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        salaryApi.del(id)
          .then((resp) => {
            if (resp && resp.code === 1) {
              this.$message.success('删除成功!')
              this.onSearch()
            } else {
              this.$message.error('删除失败:' + (resp.msg ? resp.msg : '原因未知'))
            }
          })
          .catch(error => {
            console.error('操作失败:', error)
          })
      }).catch(() => {})
    },
    detailRow (id) {
      this.selectedSalaryId = id
      this.salaryViewVisible = true
    },
    findEmployee () {
      this.employeeSelectorVisible = true
    },
    handleEmployeeSelectedEvent (selectedEmployee) {
      this.employeeSelectorVisible = false
      if (selectedEmployee && 'employeeId' in selectedEmployee && this.searchParams.employeeId !== selectedEmployee['employeeId']) {
        this.searchParams.employeeId = selectedEmployee['employeeId']
        this.searchParams.employeeName = selectedEmployee['employeeName']
      }
    },
    handleCloseSalaryViewEvent () {
      this.salaryViewVisible = false
    },
    handleResetSalaryAddEvent () {
      this.salaryAddTitle = '员工薪资新增'
    },
    handleCloseSalaryAddEvent (params) {
      if (params && params.search) {
        this.onSearch()
      }
      this.salaryAddVisible = false
    }
  }
}
</script>

<style lang="scss">
</style>